1849C - Binary String Copying - CodeForces Solution


data structures hashing strings

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10;
const int P=998244353;
int n,m,T,cnt,ans;
string a;
int sum1[N],sum2[N],pre[N],nx[N];
set<pair<int,int>> s;
void solve(){
    cin>>n>>m;
    cin>>a;
    s.clear();
    a=" "+a;
    ans=0;
    for(int i=1;i<=n;i++){
        pre[i]=pre[i-1];
        if(a[i]=='0') pre[i]=i;
    }
    nx[n+1]=n+1;
    for(int i=n;i>=1;i--){
        nx[i]=nx[i+1];
        if(a[i]=='1') nx[i]=i;
    }
    while(m--){
        int l,r;
        cin>>l>>r;
        l=nx[l],r=pre[r];
        if(l>=r) ans=1;
        else {
            s.insert(make_pair(l,r));
        }
    }
    cout<<ans+s.size()<<"\n";
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>T;
    //T=1;
    while(T--){
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1438A - Specific Tastes of Andre
1711C - Color the Picture
1194C - From S To T
110B - Lucky String
1114A - Got Any Grapes
224B - Array
125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points
762C - Two strings
802M - April Fools' Problem (easy)
577B - Modulo Sum
1555B - Two Tables
1686A - Everything Everywhere All But One
1469B - Red and Blue
1257B - Magic Stick